package com.tongxin.van.frameworkcanary.base;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;

/**
 *
 * author: VanyLiu
 * email: 13212674795@163.com
 * description: Activity基类
 *
**/
public abstract class BaseActivity<T extends ViewDataBinding> extends AppCompatActivity {

    protected T mBinding;
    protected Bundle savedInstanceState; // 高德地图调用onCreate(savedInstanceState)方法

    @Override
    protected void onStart() {
        super.onStart();
        ActivityManager.getManager().pushActivity(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.savedInstanceState = savedInstanceState;

        mBinding = DataBindingUtil.setContentView(this, layoutId());

        try {
            getBundleData(getIntent().getExtras());
        } catch (Exception e) {
            Toast.makeText(this, "参数错误", Toast.LENGTH_SHORT).show();
            finish();
        }

        initTitlebar();

        initViews();

        loadData();
    }

    /**
     * 返回布局id
     */
    protected abstract int layoutId();

    /**
     * 获取上个页面传过来的数据
     */
    protected abstract void getBundleData(Bundle bundle);

    /**
     * 初始化标题栏，所有标题栏相关的代码都写在这里
     */
    protected abstract void initTitlebar();

    /**
     * 初始化View（设置控件样式、初始值、设置监听事件等）
     */
    protected abstract void initViews();

    /**
     * 获取数据
     */
    protected abstract void loadData();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManager.getManager().popActivity(this);
    }

    /**
     * 启动Activity
     */
    protected void startActivity(Class<? extends Activity> cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }

    /**
     * 启动Activity,带Bundle参数
     */
    protected void startActivity(Class<? extends Activity> cls, @NonNull Bundle bundle) {
        Intent intent = new Intent(this, cls);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 启动Activity,带请求码
     */
    protected void startActivityForResult(Class<? extends Activity> cls, int requestCode) {
        Intent intent = new Intent(this, cls);
        startActivityForResult(intent, requestCode);
    }

    /**
     * 启动Activity,带请求码,带Bundle参数
     */
    protected void startActivityForResult(Class<? extends Activity> cls, int requestCode, @NonNull Bundle bundle) {
        Intent intent = new Intent(this, cls);
        intent.putExtras(bundle);
        startActivityForResult(intent, requestCode);
    }

    /**
     * 关闭下拉刷新动画或上拉加载更多动画
     *
     * @param refreshLayout
     */
    protected void onAnimationFinish(SmartRefreshLayout refreshLayout) {
        if (null != refreshLayout) {
            if (refreshLayout.isEnableRefresh()) {
                refreshLayout.finishRefresh();
                refreshLayout.setNoMoreData(false);//恢复上拉状态
            }
            if (refreshLayout.isEnableLoadMore()) {
                refreshLayout.finishLoadMore();
            }
        }
    }

    /**
     * 固定字体大小
     *
     * @return
     */
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }
}
